package com.sinder.campus_prophet.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 按指定时间获取当前时间周数
 * 周数从周一到周日计算(校历)
 * 周次(星期)从周日到周六计算为一周(国际)
 * @author v-xyingde
 * @date 2023/3/24/12:13
 */
public class WeekUtils {
    /**
     * 一周七天
     */
    private static final int DAYS_PER_WEEK = 7;

    /**
     * 获取周数
     * @param startDate
     * @param currentDate
     * @return
     */
    public static int getCurrentWeekNumber(Date startDate, Date currentDate) {
        // 默认从第一周开始
        int weekNumber = 1;

        // 计算从开始时间到当前时间的天数
        long daysBetween = getDaysBetween(startDate, currentDate);
        // 计算当前时间是第几周
        if (daysBetween >= DAYS_PER_WEEK) {
            weekNumber = (int) (daysBetween / DAYS_PER_WEEK) + 1;
        }

        return weekNumber;
    }

    /**
     * 获取当前星期
     * @param currentDate
     * @return
     */
    public static int getCurrentDayOfWeek(Date currentDate) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    private static long getDaysBetween(Date startDate, Date currentDate) {
        long startTime = startDate.getTime();
        long currentTime = currentDate.getTime();
        return (currentTime - startTime) / (1000 * 60 * 60 * 24);
    }

    public static void main(String[] args) {
        String startDateStr = "2023-02-20";
        String currentDateStr = "2023-03-24";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date currentDate = null;
        try {
            startDate = dateFormat.parse(startDateStr);
            currentDate = dateFormat.parse(currentDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int currentWeekNumber = getCurrentWeekNumber(startDate, currentDate);
        int currentDayOfWeek = getCurrentDayOfWeek(currentDate);

        System.out.println("当前时间是第 " + currentWeekNumber + " 周，周" + currentDayOfWeek);
    }
}
